package com.scott.crm.role.service.impl;

import com.scott.crm.role.bean.UserRoleBean;
import com.scott.crm.role.dao.RoleDao;
import com.scott.crm.role.dao.UserRoleDao;
import com.scott.crm.role.service.RoleService;
import com.scott.crm.role.service.UserRoleService;
import com.scott.crm.student.bean.StudentBean;
import com.scott.crm.student.bean.StudentDto;
import com.scott.crm.student.dao.StudentDao;
import com.scott.crm.student.service.StudentService;
import com.scott.crm.teacher.bean.TeacherBean;
import com.scott.crm.teacher.dao.TeacherDao;
import com.scott.crm.teacher.service.TeacherService;
import com.scott.crm.user.bean.UserBean;
import com.scott.crm.user.dao.UserDao;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 王伟雄
 * @create 2021/11/29 15:35
 */
@Service
public class UserRoleServiceImpl implements UserRoleService {
    @Autowired
    private UserRoleDao dao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private StudentService studentService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private StudentDao studentDao;

    @Autowired
    private TeacherDao teacherDao;

    @Autowired
    private RoleDao roleDao;
    @Override
    public Integer add(UserRoleBean userRoleBean) throws Exception {
        UserRoleBean bean=new UserRoleBean();
        bean.setUserId(userRoleBean.getUserId());
        // 查询表中是否已有该用户，如果有则添加失败
        if(dao.select(bean).size()>0){
            return 0;
        }
        Integer roleId = userRoleBean.getRoleId();
        String roleCode = roleDao.selectByPrimaryKey(roleId).getRoleCode();

        // 如果绑定的角色为学生，则在学生表插入数据
        if(roleCode.equals("student") || roleCode.equals("class-administrator")){
            StudentBean studentBean=new StudentBean();
            studentBean.setId(userRoleBean.getUserId());
            studentService.addStudent(studentBean);
            // 如果绑定的角色为老师，则在老师表插入数据
        }else if(roleCode.equals("teacher")){
            TeacherBean teacherBean=new TeacherBean();
            teacherBean.setTeacherId(userRoleBean.getUserId());
            teacherService.add(teacherBean);
        }
        return dao.insert(userRoleBean);
    }

    @Override
    public Integer delete(List<Integer> ids) {
        for(Integer id:ids){
            // 通过id找到用户id
            if(id==null){
                continue;
            }
            UserRoleBean userRole = dao.selectByPrimaryKey(id);
            Integer userId = userRole.getUserId();

            // 通过用户id找到角色名字
            UserBean userBean=new UserBean();
            userBean.setId(userId);
            List<UserBean> userBeans = userDao.loginLoading(userBean);

            // 用角色名判断条件，进行老师表学生表删除
            String roleName = userBeans.get(0).getRoleName();
            if(roleName.equals("学生") || roleName.equals("班长")){
                studentDao.deleteByPrimaryKey(userId);
            }else if(roleName.equals("老师")){
                teacherDao.deleteByPrimaryKey(userId);
            }
        }
        return  dao.deleteUserRole(ids);
    }

    @Override
    public List<UserRoleBean> findAll( Integer begin,  Integer end) {
        List<UserRoleBean> userRoleBeans = dao.findAllLimit(begin, end);
        for(UserRoleBean userRoleBean:userRoleBeans){
            if(userRoleBean.getRoleName()==null){
                userRoleBean.setRoleName("待分配角色");
            }
        }

        return userRoleBeans;
    }

    @Override
    public List<UserRoleBean> findAllLimitByRoleName( Integer begin, Integer end,UserRoleBean userRoleBean) {
        if(userRoleBean.getRoleName().equals("")){
            List<UserRoleBean> userRoleBeans = dao.findAllLimit(begin, end);
            for(UserRoleBean bean:userRoleBeans){
                if(userRoleBean.getRoleName()==null){
                    bean.setRoleName("待分配角色");
                }
            }
            return userRoleBeans;
        }else {
            List<UserRoleBean> userRoleBeans = dao.findAllLimitByRoleName(begin, end,userRoleBean);
            for(UserRoleBean bean:userRoleBeans){
                if(userRoleBean.getRoleName()==null){
                    bean.setRoleName("待分配角色");
                }
            }
            return userRoleBeans;
        }
    }

    @Override
    public List<UserRoleBean> findAll() {
        return dao.findAll();
    }

    @Override
    public List<UserRoleBean> findAllByRoleName(UserRoleBean userRoleBean) {
        return dao.findAllByRoleName(userRoleBean);
    }


}
